package com.jsdsft.openapi.controller;

import com.alibaba.fastjson2.JSONObject;
import com.jsdsft.openapi.rest.JsdSftRestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 交易收单相关接口
 *
 * @author xm
 * @date 2025/1/14
 **/
@RestController
@RequestMapping("ss_order")
public class SsOrderController {

    @Autowired
    private JsdSftRestUtil jsdSftRestUtil;
    private final static String version = "/v1";

    @Value("${jsd_sft.mc_no}")
    public String mc_no;
    @Value("${jsd_sft.pay_callback}")
    public String pay_callback;
    @Value("${jsd_sft.refund_callback}")
    public String refund_callback;
    @Value("${jsd_sft.commission_callback}")
    public String commission_callback;
    @GetMapping("jsapi")
    public Object jsapi() {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("mcNo", mc_no);
        requestMap.put("orderNo", "202401230000001");
        requestMap.put("amount", "1");
        requestMap.put("payMethod", "ALIPAY");
        requestMap.put("paySource", "MINI");
        requestMap.put("remark", "测试一下");
        requestMap.put("goodsName", "旺仔牛奶一瓶");
        requestMap.put("appId", "appId");
        requestMap.put("openId", "openId");
        requestMap.put("payTimeLimit", "2025-1-24 00:00:00");
        requestMap.put("extraCommonParam", "测试回调参数");
        requestMap.put("notifyUrl", pay_callback);
        requestMap.put("payerIp", "");
        requestMap.put("isDiv", "Y"); // 是否需要分账：Y-需要，N-不需要，如果设置需要，支付完毕需要商户自主调用订单完结接口才能解冻资金到商户账户
        try {
            JSONObject jsonObject = jsdSftRestUtil.post(requestMap, version + "/ss_order/jsapi");

            if (!"Y000".equals(jsonObject.getString("code"))) {
                // todo 这是网关非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("msg"));
            }

            if (!"S000".equals(jsonObject.getString("subCode"))) {
                // todo 这是业务非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("subMsg"));
            }

            // todo 拿到业务数据，继续业务处理即可，例如保存单号，然后返回给前端唤起支付等
            jsonObject.put("data", jsonObject.getJSONObject("data"));
            return jsonObject;
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @GetMapping("h5")
    public Object h5() {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("mcNo", mc_no);
        requestMap.put("orderNo", "202401230000001");
        requestMap.put("amount", "0.01");
        requestMap.put("payMethod", "ALIPAY");
        requestMap.put("remark", "测试一下");
        requestMap.put("goodsName", "旺仔牛奶一瓶");
        requestMap.put("payTimeLimit", "2025-03-24 00:00:00");
        requestMap.put("extraCommonParam", "测试回调参数");
        requestMap.put("notifyUrl", pay_callback);
        requestMap.put("returnUrl", "https://www.baidu.com");
        requestMap.put("payerIp", "");
        requestMap.put("isDiv", "Y"); // 是否需要分账：Y-需要，N-不需要，如果设置需要，支付完毕需要商户自主调用订单完结接口才能解冻资金到商户账户
        try {
            JSONObject jsonObject = jsdSftRestUtil.post(requestMap, version + "/ss_order/h5");

            if (!"Y000".equals(jsonObject.getString("code"))) {
                // todo 这是网关非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("msg"));
            }

            if (!"S000".equals(jsonObject.getString("subCode"))) {
                // todo 这是业务非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("subMsg"));
            }

            // todo 拿到业务数据，继续业务处理即可，例如保存单号，然后返回给前端唤起支付等
            jsonObject.put("data", jsonObject.getJSONObject("data"));
            return jsonObject;
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @GetMapping("pc")
    public Object pc() {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("mcNo", mc_no);
        requestMap.put("orderNo", "202401230000001");
        requestMap.put("amount", "0.01");
        requestMap.put("payMethod", "ALIPAY");
        requestMap.put("remark", "测试一下");
        requestMap.put("goodsName", "旺仔牛奶一瓶");
        requestMap.put("payTimeLimit", "2025-03-24 00:00:00");
        requestMap.put("extraCommonParam", "测试回调参数");
        requestMap.put("notifyUrl", pay_callback);
        requestMap.put("returnUrl", "https://www.baidu.com");
        requestMap.put("payerIp", "");
        requestMap.put("isDiv", "Y"); // 是否需要分账：Y-需要，N-不需要，如果设置需要，支付完毕需要商户自主调用订单完结接口才能解冻资金到商户账户
        try {
            JSONObject jsonObject = jsdSftRestUtil.post(requestMap, version + "/ss_order/pc");

            if (!"Y000".equals(jsonObject.getString("code"))) {
                // todo 这是网关非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("msg"));
            }

            if (!"S000".equals(jsonObject.getString("subCode"))) {
                // todo 这是业务非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("subMsg"));
            }

            // todo 拿到业务数据，继续业务处理即可，例如保存单号，然后返回给前端唤起支付等
            jsonObject.put("data", jsonObject.getJSONObject("data"));
            return jsonObject;
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @GetMapping("query_order")
    public Object query_order() {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("mcNo", mc_no);
        requestMap.put("orderNo", "202401230000001");
        requestMap.put("tradeNo", "S2025031492131000001");
        try {
            JSONObject jsonObject = jsdSftRestUtil.post(requestMap, version + "/ss_order/query_order");

            if (!"Y000".equals(jsonObject.getString("code"))) {
                // todo 这是网关非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("msg"));
            }

            if (!"S000".equals(jsonObject.getString("subCode"))) {
                // todo 这是业务非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("subMsg"));
            }

            // todo 拿到业务数据，继续业务处理即可，例如保存单号，然后返回给前端唤起支付等
            jsonObject.put("data", jsonObject.getJSONObject("data"));
            return jsonObject;
        } catch (Exception e) {
            return e.getMessage();
        }
    }


    @GetMapping("cancel")
    public Object cancel() {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("mcNo", mc_no);
        requestMap.put("orderNo", "202401230000001");
        requestMap.put("tradeNo", "S2025011674312000008");
        try {
            JSONObject jsonObject = jsdSftRestUtil.post(requestMap, version + "/ss_order/cancel");

            if (!"Y000".equals(jsonObject.getString("code"))) {
                // todo 这是网关非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("msg"));
            }

            if (!"S000".equals(jsonObject.getString("subCode"))) {
                // todo 这是业务非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("subMsg"));
            }

            // todo 拿到业务数据，继续业务处理即可，例如保存单号，然后返回给前端唤起支付等
            jsonObject.put("data", jsonObject.getJSONObject("data"));
            return jsonObject;
        } catch (Exception e) {
            return e.getMessage();
        }
    }


    @GetMapping("refund")
    public Object refund() {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("mcNo", mc_no);
        requestMap.put("orderNo", "202401230000001");
        requestMap.put("tradeNo", "S2025011674312000008");
        requestMap.put("refundOrderNo", "R20240124000001");
        requestMap.put("amount", "0.1");
        requestMap.put("refundReason", "测试退款");
        requestMap.put("notifyUrl", refund_callback);
        try {
            JSONObject jsonObject = jsdSftRestUtil.post(requestMap, version + "/ss_order/refund");

            if (!"Y000".equals(jsonObject.getString("code"))) {
                // todo 这是网关非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("msg"));
            }

            if (!"S000".equals(jsonObject.getString("subCode"))) {
                // todo 这是业务非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("subMsg"));
            }

            // todo 拿到业务数据，继续业务处理即可，例如保存单号，然后返回给前端唤起支付等
            jsonObject.put("data", jsonObject.getJSONObject("data"));
            return jsonObject;
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @GetMapping("query_refund_order")
    public Object query_refund_order() {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("mcNo", mc_no);
        requestMap.put("orderNo", "202401230000001");
        requestMap.put("tradeNo", "S2025011679661000004");
        requestMap.put("refundOrderNo", "R20240124000001");
        requestMap.put("tradeRefundOrderNo", "R2025011644757000002");
        try {
            JSONObject jsonObject = jsdSftRestUtil.post(requestMap, version + "/ss_order/query_refund_order");

            if (!"Y000".equals(jsonObject.getString("code"))) {
                // todo 这是网关非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("msg"));
            }

            if (!"S000".equals(jsonObject.getString("subCode"))) {
                // todo 这是业务非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("subMsg"));
            }

            // todo 拿到业务数据，继续业务处理即可，例如保存单号，然后返回给前端唤起支付等
            jsonObject.put("data", jsonObject.getJSONObject("data"));
            return jsonObject;
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @GetMapping("order_div")
    public Object order_div() {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("mcNo", mc_no);
        requestMap.put("orderNo", "202401230000001");
        requestMap.put("tradeNo", "S2025031460787000003");
        requestMap.put("divOrderNo", "202401230000001");
        requestMap.put("divNo", "o2S1I7OwDZpAkAOkHUtuv9D9KK-U");
        requestMap.put("divNoType", "PERSONAL_OPENID");
        requestMap.put("amount", "0.01");
        requestMap.put("remark", "测试分账");
        requestMap.put("notifyUrl", commission_callback);
        try {
            JSONObject jsonObject = jsdSftRestUtil.post(requestMap, version + "/ss_order/order_div");

            if (!"Y000".equals(jsonObject.getString("code"))) {
                // todo 这是网关非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("msg"));
            }

            if (!"S000".equals(jsonObject.getString("subCode"))) {
                // todo 这是业务非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("subMsg"));
            }

            // todo 拿到业务数据，继续业务处理即可，例如保存单号，然后返回给前端唤起支付等
            jsonObject.put("data", jsonObject.getJSONObject("data"));
            return jsonObject;
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @GetMapping("order_div_query")
    public Object order_div_query() {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("mcNo", mc_no);
        requestMap.put("divOrderNo", "202401230000001");
        requestMap.put("divTradeNo", "D2025031486192000016");
        try {
            JSONObject jsonObject = jsdSftRestUtil.post(requestMap, version + "/ss_order/order_div_query");

            if (!"Y000".equals(jsonObject.getString("code"))) {
                // todo 这是网关非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("msg"));
            }

            if (!"S000".equals(jsonObject.getString("subCode"))) {
                // todo 这是业务非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("subMsg"));
            }

            // todo 拿到业务数据，继续业务处理即可，例如保存单号，然后返回给前端唤起支付等
            jsonObject.put("data", jsonObject.getJSONObject("data"));
            return jsonObject;
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @GetMapping("order_unfreeze")
    public Object order_unfreeze() {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("mcNo", mc_no);
        requestMap.put("orderNo", "202401230000001");
        requestMap.put("tradeNo", "S2025031460787000003");
        try {
            JSONObject jsonObject = jsdSftRestUtil.post(requestMap, version + "/ss_order/order_unfreeze");

            if (!"Y000".equals(jsonObject.getString("code"))) {
                // todo 这是网关非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("msg"));
            }

            if (!"S000".equals(jsonObject.getString("subCode"))) {
                // todo 这是业务非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("subMsg"));
            }

            // todo 拿到业务数据，继续业务处理即可，例如保存单号，然后返回给前端唤起支付等
            jsonObject.put("data", jsonObject.getJSONObject("data"));
            return jsonObject;
        } catch (Exception e) {
            return e.getMessage();
        }
    }


    @GetMapping("commission_user_add")
    public Object commission_user_add() {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("mcNo", mc_no);
        requestMap.put("payMethod", "ALIPAY");
        requestMap.put("divNo", "18321112155");
        requestMap.put("divNoType", "loginName");
        requestMap.put("name", "宋测试");
        requestMap.put("appId", "xxxxxx");
        requestMap.put("remark", "测试");
        requestMap.put("relationType", "STAFF");
        requestMap.put("customRelation", "");
        try {
            JSONObject jsonObject = jsdSftRestUtil.post(requestMap, version + "/ss_order/commission_user_add");

            if (!"Y000".equals(jsonObject.getString("code"))) {
                // todo 这是网关非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("msg"));
            }

            if (!"S000".equals(jsonObject.getString("subCode"))) {
                // todo 这是业务非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("subMsg"));
            }

            // todo 拿到业务数据，继续业务处理即可，例如保存单号，然后返回给前端唤起支付等
            jsonObject.put("data", jsonObject.getJSONObject("data"));
            return jsonObject;
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @GetMapping("commission_user_delete")
    public Object commission_user_delete() {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("mcNo", mc_no);
        requestMap.put("bdId", "bc068da9ebc3bba702a2cca1a5250de6");
        try {
            JSONObject jsonObject = jsdSftRestUtil.post(requestMap, version + "/ss_order/commission_user_delete");

            if (!"Y000".equals(jsonObject.getString("code"))) {
                // todo 这是网关非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("msg"));
            }

            if (!"S000".equals(jsonObject.getString("subCode"))) {
                // todo 这是业务非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("subMsg"));
            }

            // todo 拿到业务数据，继续业务处理即可，例如保存单号，然后返回给前端唤起支付等
            jsonObject.put("data", jsonObject.getJSONObject("data"));
            return jsonObject;
        } catch (Exception e) {
            return e.getMessage();
        }
    }
}
